﻿/*
 * Created by SharpDevelop.
 * User: Михаил
 * Date: 28.07.2013
 * Time: 15:18
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Collections.Generic;
using Region.Utils;
namespace Region.Forms
{
	/// <summary>
	/// Description of Pred.
	/// </summary>
	public partial class PredForm		: Form, IDBForm
	{
		private Engine m_engine;
    private Pred m_pred;
    ObjectFormUtil m_reader;
    private Kont m_kont;
    public ObjectFormUtil i_reader{get{return m_reader;}}
    public DBObject i_object{get{return m_pred;}}
    public void readObject()
    {
      this.Text = "Редактирование предписания ФБУЗ №" + m_pred.PredNum.ToString() + " от " + m_pred.PredDate.ToShortDateString();
      m_reader.get();
      if (m_pred.KontID != 0)
      {
        m_kont = m_pred.Kont;
        KontName.Text = m_kont.KontName;
        INN.Text = m_kont.INN;
        OGRN.Text = m_kont.OGRN;
        KontGosReg.Text = m_kont.KontGosReg.ToShortDateString();
        KontForm.Text = m_engine.KontForm[m_kont.KontForm];
        KontType.Text = m_engine.KontType[m_kont.KontTypeID];
        ULAddress.Text = m_kont.KontULAddress;
        FKAddress.Text = m_kont.KontFKAddress;
        //AddKontObject.Enabled = m_pred.poleAccess("KontID");
        //KontObjectList.Items.Clear();
        //Dictionary<Int32, String> Raions = m_engine.Raions;
        //Dictionary<Int32, Raspor.RasporObject> RO = m_raspor.RasporObjects;
        /*foreach (KeyValuePair<Int32, Kont.KontObject> K in m_kont.KontObjects)
        {
          //User U = m_engine.getUser();
          Int32 KontObjectID = K.Key;
          String S = K.Value.KontObjectName + ": " + Engine.getAddressString(K.Value.KontObjectPostIndex, K.Value.KontObjectRegion, Raions[K.Value.KontObjectRaionID], K.Value.KontObjectGorod, K.Value.KontObjectNaspunkt, K.Value.KontObjectStreet, K.Value.KontObjectDom, K.Value.KontObjectKorpus, K.Value.KontObjectKvart);
          KontObjectList.Items.Add(new Utils.ListItemValue(S, KontObjectID), RO.ContainsKey(KontObjectID));
        }*/
      }
      
      ServiceList.Items.Clear();
      foreach (KeyValuePair<Int32, String> RU in m_engine.Service)
      {
        var item = new ListItemValue(RU.Value,RU.Key);
        ServiceList.Items.Add(item);
      } 
      TaskList.Items.Clear();
      foreach (KeyValuePair<Int32, String> RU in m_engine.TaskVCP)
      {
        var item = new ListItemValue(RU.Value,RU.Key);
        TaskList.Items.Add(item);
      } 
    }
    public void checkObject()
    {
    }
    public void saveObject()
    {
      m_reader.set();
      m_pred.save();
    }
    public PredForm(Engine engine,Int32 PredID)
		{
      m_engine = engine;
      m_pred = m_engine.getPred(PredID);
      m_reader = new ObjectFormUtil(m_engine, m_pred);
			InitializeComponent();            
      Properties.Settings ps = Properties.Settings.Default;
      DBForm.addEditors(this,this);
     
      /*m_reader.AddEditor(PredNum, "PredNum");
      m_reader.AddEditor(PredDate, "PredDate");
      m_reader.AddEditor(StartDate, "StartDate");
      m_reader.AddEditor(EndDate, "EndDate");      
/*            m_reader.AddEditor(RasporNum, "RasporNum");
      m_reader.AddEditor(Date, "Date");
      m_reader.AddEditor(CheckDoc, "CheckDoc");
      m_reader.AddEditor(CheckGo, "CheckGo");
      m_reader.AddEditor(CheckPlan, "CheckPlan", new Dictionary<Boolean, String>() { { false, "Внеплановая" }, { true, "Плановая" }});
      m_reader.AddEditor(CheckLicen, "CheckLicen");
      m_reader.AddEditor(Date, "Date");
      m_reader.AddEditor(IspInfo, "IspInfo");
      m_reader.AddEditor(ExpertsInfo, "ExpertsInfo");
      m_reader.AddEditor(TargetInfo, "TargetInfo");
      m_reader.AddEditor(TaskInfo, "TaskInfo");
      m_reader.AddEditor(TP, "TP",new Dictionary<Int32, String>() { { 0, "Не более 20 рабочих дней" }, { 1, "50 часов(Малый бизнес)" }, { 2, "15 часов(Микробизнес)" }});
      m_reader.AddEditor(Start, "Start");
      m_reader.AddEditor(End, "End");
      m_reader.AddEditor(OsnovID, "OsnovID",engine.Osnov); 
      m_reader.AddEditor(OsnovInfo, "OsnovInfo"); 
      m_reader.AddEditor(MKontrInfo, "MKontrInfo"); 
      m_reader.AddEditor(AReglamInfo, "AReglamInfo"); 
      m_reader.AddEditor(PDocInfo, "PDocInfo"); 
      m_reader.AddEditor(RucComment, "RucComment"); 
      m_reader.AddEditor(NachComment, "NachComment"); */            
      
      //Font = new Font(Font.FontFamily, (float)ps.RasporForm_Size);
            readObject();
		}
		
		void Label1Click(object sender, EventArgs e)
		{
			
		}
		void EditKontClick(object sender, System.EventArgs e)
		{
      if (m_pred.poleAccess("KontID"))
      {
        var KL = new Forms.RegionKontList(m_engine);
        if (KL.ShowDialog() == DialogResult.OK)
        {
            m_pred.KontID = KL.SelectKontID;
            saveObject();
            readObject();
        }
      }
		}
		void ToolStripButton1Click(object sender, EventArgs e)
		{
      string md = Environment.GetFolderPath(Environment.SpecialFolder.Personal);//путь к Документам
      if (System.IO.Directory.Exists(md + @"\Предпсание") == false)
      {
          System.IO.Directory.CreateDirectory(md + @"\Предпсание");
      }
      String FileName = md + @"\Предпсание\" + m_pred.PredNum + ".docx";
      if (m_pred.exportDOCX(FileName))
      {
        // Открыть файл во внешней программе
        var pr = new System.Diagnostics.Process();
        pr.StartInfo.FileName = FileName;
        pr.Start();
      }  
		}
		void SaveButtonClick(object sender, EventArgs e)
		{
		  saveObject();
		}
	}
}
